<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PinController extends Controller
{   
    public function pin(Request $request){
        $pinTel = $request->session()->get('eai')['tel'];
        if(!$pinTel){
            redirect('login');
        }else{
            $activeTime = DB::table('waihui_account') //如果已经激活
                        ->where('tel',$pinTel)
                        ->value('active_time');
            if($activeTime){
                redirect('/');
            }
        }
        return view('pin.pin');
    }
    
    //登录完后，强制更新密码和激活时间
    public function checkPin(Request $request){
        $pinTel = $request->session()->get('eai')['tel'];
        if(!$pinTel){
            return 2; //没有登录
        }
        $pin = intval(trim($request->get('pin'))) ? intval(trim($request->get('pin'))) : '' ;
        if(!$pin){
            return 0; //用户输入有误
        }else{
            $isexist = DB::table('waihui_account')
                       ->where([['tel','=',"$pinTel"],['pin','=',"$pin"]])
                       ->first();
            if(!$isexist){
                return 0;  //账户不存在
            }else{
                return 1; //更新成功
            }
        }
    }
    
    
    public function activate(Request $request){
        $sessionData = $request->session()->get('eai');
        if(!$sessionData){ //没有登录
            return 2;
        }
        $data['userid']     = $sessionData['id'];
        //充值密码页
        $pin = intval(trim($request->get('pin'))) ? intval(trim($request->get('pin'))) : '' ;
        $pwd = intval(trim($request->get('pwd'))) ? intval(trim($request->get('pwd'))) : '' ;
        if(!$pin || !$pwd){
            return 0;  //用户输入有误
        }else{
            $isexist = DB::table('waihui_account')
                     ->where([['tel','=',$sessionData['tel']],['pin','=',"$pin"]])
                     ->first();
            if(!$isexist){
                return 0;  //账户不存在
            }
        }
        
        //实名认证页
        $phone = trim($request->get('phone')); 
        $email = trim($request->get('email'));
        $data['realname']   = $realName = trim($request->get('realName'));
        $data['address']    = $addr = trim($request->get('addr')); 
        $data['idcardimg']  = $idcardimg = trim($request->get('idcardimg')); 
        $data['idcardimgf'] = $idcardimgf = trim($request->get('idcardimgf')); 
        $data['homeimg']    = $homeimg = trim($request->get('homeimg')); 
        $data['fuhomeimg']  = $fuhomeimg = trim($request->get('fuhomeimg'));
        $password = md5($pwd);
        
        if($realName=='' || $phone=='' || $email=='' || $addr=='' || $idcardimg=='' || $idcardimgf=='' || $homeimg==''|| $fuhomeimg==''){  //如果其中任何一个值为空',说明用户没有填写全部实名认证信息
            //更新member的密码字段
            $oldPassword = DB::table('waihui_member')->where('tel',$sessionData['tel'])->value('password'); 
            if($oldPassword != $password){
                $resMember = DB::table('waihui_member')->where('tel',$sessionData['tel'])->update(['password'=>$password]); 
            }else{
                $resMember = 1;
            }
            $resAccount = DB::table('waihui_account') //更新account表的激活时间字段
                        ->where('tel',$sessionData['tel'])
                        ->update(['active_time'=>time()]); 
            
            if(!$resMember || !$resAccount){
                return 0; //更新失败
            }else{
                return 1; //更新成功
            }
        }else if($realName!='' && $phone!='' && $email!='' && $addr!='' && $idcardimg!='' && $idcardimgf!='' && $homeimg!='' && $fuhomeimg!=''){ //用户填写了实名认证信息
                $resUpdatePhoneEmail = DB::table('waihui_member') //更新member表的phone字段和email字段和password字段
                                     ->where('tel',$sessionData['tel'])
                                     ->update(['phone'=>$phone,'email'=>$email,'password'=>$password]);
                
                $resAccount = DB::table('waihui_account') //更新account表的激活时间字段
                              ->where('tel',$sessionData['tel'])
                              ->update(['active_time'=>time()]); 
                
                if($resUpdatePhoneEmail && $resAccount){ //如果更新member表成功，那么说明此账号存在
                    $isexist = DB::table('waihui_pinmoreinfo')->where('userid',$data['userid'])->first(); //先判断有没有此记录
                    if($isexist){
                        $res = DB::table('waihui_pinmoreinfo')->where('userid',$data['userid'])->delete();
                    }
                    $resPIN = DB::table('waihui_pinmoreinfo')->insert($data); 
                    if($resPIN){
                        return 1;
                    }else{
                        return 0; 
                    }
                }else{
                    return 0; 
                }
        } 
    }
}    